<?php
require APPPATH.'third_party/phpsvnclient/phpsvnclient.php';
class svnadapter
{
    private $url;
    private $user;
    private $pass;
    private $phpsvnclient;
    
    /**
     * Codeigniter way of passing parameters
     * @param unknown_type $params
     */
    public function Svnadapter($params) {
        $this->url = $params['url'];
        $this->user = $params['user'];
        $this->pass = $params['pass'];
        $this->__construct($this->url, $this->user, $this->pass);
    }

    public function __construct($params) {
        $this->url = $params['url'];
        $this->user = $params['user'];
        $this->pass = $params['pass'];
        $this->phpsvnclient = new phpsvnclient($this->url, $this->user, $this->pass);
    }

    /**
     *  Public Functions
     */

    /**
     * Performs a checkout and creates files and folders.
     * 
     * @param string $folder Defaults to disk root
     * @param string $outPath Defaults to current folder (.)
     * @param boolean $checkFiles Whether it is necessary to check the received 
     * files in the sizes. Can be useful in case often files are accepted 
     * with an error.
     */
    public function checkOut($folder = '/', $outPath = '.', $checkFiles = false) {
        $this->phpsvnclient->checkOut($folder, $outPath, $checkFiles);
    }

    /**
     * Function to easily create and update a working copy of the repository.
     * @param type $folder Folder in remote repository
     * @param type $outPath Folder for storing files
     * @param boolean $checkFiles Whether it is necessary to check the received 
     * files in the sizes. Can be useful in case often files are accepted 
     * with an error.
     */
    public function createOrUpdateWorkingCopy($folder = '/', $outPath = '.', $checkFiles = false) {
        $this->phpsvnclient->createOrUpdateWorkingCopy($folder, $outPath, $checkFiles);
    }

    /**
     * Function to view the changes between revisions of the specified object.
     * @param type $path The path to the object (file or directory).
     * @param type $revFrom Initial revision.
     * @param type $revTo The final revision.
     */
    public function diffVersions($path = '', $revFrom = 0, $revTo = 0) {
        $this->phpsvnclient->diffVersions($path, $revFrom, $revTo);
    }

    /**
     *  rawDirectoryDump
     *
     * Dumps SVN data for $folder in the version $version of the repository.
     *
     *  @param string  $folder Folder to get data
     *  @param integer $version Repository version, -1 means actual
     *  @return array SVN data dump.
     */
    public function rawDirectoryDump($folder = '/', $version = -1) {
        $this->phpsvnclient->rawDirectoryDump($folder, $version);
    }

    /**
     *  getDirectoryFiles
     *
     *  Returns all the files in $folder in the version $version of 
     *  the repository.
     *
     *  @param string  $folder Folder to get files
     *  @param integer $version Repository version, -1 means actual
     *  @return array List of files.     
     */
    public function getDirectoryFiles($folder = '/', $version = -1) {
        $this->phpsvnclient->getDirectoryFiles($folder, $version);
    }

    /**
     *  getDirectoryTree
     *
     *   Returns the complete tree of files and directories in $folder from the
     *  version $version of the repository. Can also be used to get the info 
     *  for a single file or directory.
     *
     *  @param string  $folder Folder to get tree
     *  @param integer $version Repository version, -1 means current
     *  @param boolean $recursive Whether to get the tree recursively, or just
     *  the specified directory/file.
     *
     *  @return array List of files and directories.
     */
    public function getDirectoryTree($folder = '/', $version = -1, $recursive = true) {
        $this->phpsvnclient->getDirectoryTree($folder, $version, $recursive);
    }

    /**
     *  Returns file contents
     *
     *  @param  string  $file File pathname
     *  @param  integer $version File Version
     *  @return string  File content and information, false on error, or if a
     *                  directory is requested
     */
    public function getFile($file, $version = -1) {
        $this->phpsvnclient->getFile($file, $version);
    }

    /**
     *  Get changes logs of a file.
     *
     *  Get repository change logs between version
     *  $vini and $vend.
     *
     *  @param integer $vini Initial Version
     *  @param integer $vend End Version
     *  @return Array Respository Logs
     */
    public function getRepositoryLogs($path = "/", $vini = 0, $vend = -1) {
        $this->phpsvnclient->getRepositoryLogs($path, $vini, $vend);
    }

    /**
     *  Get changes logs of a file.
     *
     *  Get repository change of a file between version
     *  $vini and $vend.
     *
     *  @param string $file File for which to get log data
     *  @param integer $vini Initial Version
     *  @param integer $vend End Version
     *  @return array Respository Logs
     */
    public function getFileLogs($file, $vini = 0, $vend = -1) {
        $this->phpsvnclient->getFileLogs($file, $vini, $vend);
    }

    public function getLogsForUpdate($file, $vini = 0, $vend = -1, $checkvend = true) {
        $this->phpsvnclient->getLogsForUpdate($file, $vini, $vend, $checkvend);
    }

    /**
     *  Returns the repository version
     *
     *  @return integer Repository version
     *  @access public
     */
    public function getVersion() {
        $this->phpsvnclient->getVersion();
    }

    /**
     *  Deprecated functions for backward comatability
     */

    /**
     *  Set URL
     *
     *  Set the project repository URL.
     *
     *  @param string $url URL of the project.
     *  @access public
     */
    public function setRepository($url) {
        $this->phpsvnclient->setRepository($url);
    }

    /**
     *  Old method; there's a typo in the name. This is now a wrapper for setRepository
     */
    public function setRespository($url) {
        $this->phpsvnclient->setRepository($url);
    }

    /**
     *  Add Authentication  settings
     *
     *  @param string $user Username
     *  @param string $pass Password
     */
    public function setAuth($user, $pass) {
        $this->phpsvnclient->setAuth($user, $pass);
    }
}